//////////////////////////////////////////////////////////////////////
//
//	Copyright 2010-2011 Rogo's Team. All Rights Reserved
//
//////////////////////////////////////////////////////////////////////
package com.rogo.weibo.platform.netease.api.search
{
	import com.rogo.weibo.core.DoAPI;
	import com.rogo.weibo.platform.netease.api.oauth.Oauth;
	import com.rogo.weibo.utils.HttpParameter;
	import com.rogo.weibo.utils.OauthUrlUtil;
	
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.navigateToURL;
	
	import json.JSON;
	
	/**
	 * 网易微博API 
	 * 1.1.12 搜索
	 * 
	 * @see http://rogo.sinaapp.com
	 * @see http://open.t.163.com
	 * @author rogo
	 */	
	public class DoSearch extends DoAPI
	{
		public function DoSearch(dataHandler:Function, errorHandler:Function)
		{
			super(dataHandler, errorHandler);
		}
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		private static const URL_SEARCH:String = 'http://api.t.163.com/1/search.json';
		public static const CMD_SEARCH:String = 'cmdSearch';
		
		private static const URL_SEARCH_STATUS:String = 'http://api.t.163.com/1/statuses/search.json';
		public static const CMD_SEARCH_STATUS:String = 'cmdSearchStatus';
		
		private static const URL_SEARCH_USER:String = 'http://api.t.163.com/1/users/search.json';
		public static const CMD_SEARCH_USER:String = 'cmdSearchUser';
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 搜索
		 * 
		 * @param q 必选参数，关键字,最大长度25,如果以#起始的关键字会作为tag搜索精确匹配
		 * @param page 可选参数，当前页数，默认为第一页
		 * @param per_page 可选参数，返回数量,最大20
		 */		
		public function search(q:String, page:String, per_page:String):void
		{
			var paras:Array = [];
			if (page != null)
				paras.push(new HttpParameter('page', page));
			if (per_page != null)
				paras.push(new HttpParameter('per_page', per_page));
			if (q != null)
				paras.push(new HttpParameter('q', q));
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_SEARCH, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_SEARCH, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_SEARCH, params);
			}
		}
		
		/**
		 * 搜索微博
		 * 
		 * @param q 必选参数，关键字,最大长度25,如果以#起始的关键字会作为tag搜索精确匹配
		 * @param page 可选参数，当前页数，默认为第一页
		 * @param per_page 可选参数，返回数量,最大20
		 * @param trim_user 可选参数，值为true时返回的user对象只包含id属性，该属性能在一定程度上减少返回的数据量
		 */		
		public function searchStatus(q:String, page:String, per_page:String, trim_user:Boolean):void
		{
			var paras:Array = [];
			if (page != null)
				paras.push(new HttpParameter('page', page));
			if (per_page != null)
				paras.push(new HttpParameter('per_page', per_page));
			if (q != null)
			{
				q = encodeURI(q);
				paras.push(new HttpParameter('q', q));
			}
			paras.push(new HttpParameter('trim_user', trim_user.toString()));
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_SEARCH_STATUS, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_SEARCH_STATUS, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_SEARCH_STATUS, params);
			}
		}
		
		/**
		 * 搜索用户
		 * 
		 * @param q 必选参数，关键字,最大长度25,如果以#起始的关键字会作为tag搜索精确匹配
		 * @param page 可选参数，当前页数，默认为第一页
		 * @param per_page 可选参数，返回数量,最大20
		 */		
		public function searchUser(q:String, page:String, per_page:String):void
		{
			var paras:Array = [];
			if (page != null)
				paras.push(new HttpParameter('page', page));
			if (per_page != null)
				paras.push(new HttpParameter('per_page', per_page));
			if (q != null)
			{
				q = encodeURI(q);
				paras.push(new HttpParameter('q', q));
			}
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			var url:String = OauthUrlUtil.getOauthUrl(URL_SEARCH_USER, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_SEARCH_USER, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_SEARCH_USER, params);
			}
		}
		
		private function resultResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_dataHandler(cmd, data);
		}
		
		private function errorResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_errorHandler(cmd, data);
		}
	}
}